<template>
  <div>
    <van-nav-bar
      title="修改地址"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
    />
    <div class="content">
      <van-address-edit
        :address-info="editingContact"
        :area-list="area"
        show-postal
        show-delete
        show-set-default
        :area-columns-placeholder="['请选择', '请选择', '请选择']"
        @save="onSave"
        @delete="onDelete"
      />
    </div>
  </div>
</template>

<script>
import { getOneAddress, delOneAddress, updateAddress } from '@api/home'
import area from '@utils/area'
export default {
  name: 'contactedit',
  data () {
    return {
      area,
      editingContact: {}
    }
  },
  created () {
    this.init()
  },
  methods: {
    init () {
      getOneAddress(this.$route.query.id).then((res) => {
        if (res.data.code === 200) {
          this.editingContact = JSON.parse(res.data.data.content)
          this.editingContact.id = this.$route.query.id - 0
        }
      })
    },
    onClickLeft () {
      this.$router.replace('/contactlist')
    },
    onSave (item) {
      updateAddress(item).then((res) => {
        if (res.data.code === 200) {
          this.$toast('更新成功')
          this.$router.replace('/contactlist')
        } else {
          this.$toast('更新失败')
        }
      })
    },
    onDelete () {
      delOneAddress(this.editingContact.id).then((res) => {
        if (res.data.code === 200) {
          this.$toast('删除成功')
          this.$router.replace('/contactlist')
        } else {
          this.$toast('删除失败')
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
